package com.ruoyi.web.controller.material.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.Page;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.web.controller.material.entity.YlProduct;
import com.ruoyi.web.controller.material.entity.dto.YlProductDto;
import com.ruoyi.web.controller.material.entity.request.ItemAddRequest;
import com.ruoyi.web.controller.material.entity.request.ItemRequest;
import com.ruoyi.web.controller.material.entity.ylSupplier;
import com.ruoyi.web.controller.material.mapper.SupplierMapper;
import com.ruoyi.web.controller.material.mapper.YlProductCategoryMapper;
import com.ruoyi.web.controller.material.mapper.YlProductMapper;
import com.ruoyi.web.controller.material.service.ItemService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 物品管理Service业务层处理
 *
 * @author jzx
 * @date 2025年10月21日16:01:44
 */
@Service
public class ItemServiceImpl implements ItemService {
    @Resource
    private YlProductMapper ylProductMapper;

    @Resource
    private YlProductCategoryMapper ylProductCategoryMapper;

    @Resource
    private SupplierMapper supplierMapper;

    /**
     * 物资分类列表查询
     */
    @Override
    public List<YlProductDto> getPage(ItemRequest itemRequest) {
        List<YlProduct> list = ylProductMapper.getPage(itemRequest);

        List<YlProductDto> listDto = BeanUtil.copyToList(list, YlProductDto.class);
        if (listDto != null) {
            for (YlProductDto YlProductDto : listDto) {
                String productCategoryName = ylProductCategoryMapper.getProductCategoryName(YlProductDto.getProductCategoryId());
                String chargeItemName = "";
                String supplierName = supplierMapper.getSupplierName(YlProductDto.getSupplierId());
                YlProductDto.setProductCategoryName(productCategoryName);
                YlProductDto.setChargeItemName(chargeItemName);
                YlProductDto.setSupplierName(supplierName);
            }
        }
        if (list instanceof Page) {
            Page<YlProduct> page = (Page<YlProduct>) list;
            Page<YlProductDto> dtoPage = new Page<>();
            dtoPage.addAll(listDto);
            dtoPage.setTotal(page.getTotal());
            dtoPage.setPageNum(page.getPageNum());
            dtoPage.setPageSize(page.getPageSize());
            return dtoPage;
        }
        return listDto;
        //YlProductDto
    }

    /**
     * 物资启用禁用
     */
    @Override
    public void updateStatus(String id, String useStatus) {

        ylProductMapper.update(
                null, // entity 为 null，表示不靠实体更新
                new LambdaUpdateWrapper<YlProduct>()
                        .eq(YlProduct::getId, id)
                        .set(YlProduct::getUseStatus, useStatus)
        );

    }

    /**
     * 物资新增
     */
    @Override
    public void add(ItemAddRequest itemAddRequest) {
        if (ylProductMapper.getProductName(itemAddRequest.getProductName(), null)) {
            throw new ServiceException("物品名称已存在");
        }
        YlProduct ylProduct = new YlProduct();
        BeanUtil.copyProperties(itemAddRequest, ylProduct);
        ylProductMapper.insert(ylProduct);
    }

    /**
     * 物资查询
     */
    @Override
    public YlProduct selectById(Long id) {
        return ylProductMapper.selectById(id);
    }

    /**
     * 物资修改
     */
    @Override
    public void update(ItemAddRequest itemUpdateRequest) {
        if (ylProductMapper.getProductName(itemUpdateRequest.getProductName(), String.valueOf(itemUpdateRequest.getId()))) {
            throw new ServiceException("物品名称已存在");
        }
        ylProductMapper.update(null,
                new LambdaUpdateWrapper<YlProduct>()
                        .eq(YlProduct::getId, itemUpdateRequest.getId())
                        .set(YlProduct::getProductName, itemUpdateRequest.getProductName())
                        .set(YlProduct::getProductCategoryId, itemUpdateRequest.getProductCategoryId())
                        .set(YlProduct::getChargeItemId, itemUpdateRequest.getChargeItemId())
                        .set(YlProduct::getSupplierId, itemUpdateRequest.getSupplierId())
                        .set(YlProduct::getSpecification, itemUpdateRequest.getSpecification())
                        .set(YlProduct::getUnit, itemUpdateRequest.getUnit())
                        .set(YlProduct::getUseStatus, itemUpdateRequest.getUseStatus())
                        .set(YlProduct::getRemark, itemUpdateRequest.getRemark())
                        .set(YlProduct::getShortName, itemUpdateRequest.getShortName())

        );
    }

    /**
     * 物资删除
     */
    @Override
    public void delete(Long[] id) {
        for (Long i : id) {
            ylProductMapper.deleteById(i);
        }
    }
}
